package com.mimiwang.oss.controller;


import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.mimiwang.common.utils.IOUtil;
import com.mimiwang.common.utils.ResponseUtil;
import com.mimiwang.oss.service.OssFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * @AUTHOR:游李滨
 * @DATE:2022/10/2 21:33
 */
@RestController
@RequestMapping("/oss")
public class OssController {


    @Autowired
    private OssFileService ossFileService;


    @GetMapping("/sign")
    public String getSignature(){
        Map<String, String> signature = ossFileService.getSignature();
        if(signature!=null){
            return ResponseUtil.successJSONData(signature);
        }
        return ResponseUtil.errorJSONData("获取失败");
    }

    /**
     *
     * 参数：String bucketName,String objectName,boolean download  1 下载原件 / 0 返回url
     *
     *
     * */
    @PostMapping("/download")
    public String download(@RequestBody Map<String,Object> param, HttpServletResponse response) throws IOException {
        String bucketName =(String) param.get("bucketName");
        String objectName =(String) param.get("objectName");
        boolean download = (Boolean)param.get("download");
        if(download){

            //      TODO  处理流 ...

            InputStream inputStream = ossFileService.downloadFile(bucketName, objectName);
            ServletOutputStream write = response.getOutputStream();
            byte[] bytes = IOUtil.readInputStream(inputStream);
            write.write(bytes);

        }else{
            if(objectName!=null){
                return ResponseUtil.successJSONData("请求成功",ossFileService.getUrl(objectName));
            }else{
                return ResponseUtil.errorJSONData("请求失败:必须指定文件名");
            }

        }
        return ResponseUtil.successJSONData("");

    }

    @RequestMapping(value = "/upload",method = {RequestMethod.GET,RequestMethod.POST})
    public String upload(MultipartFile file,
                         Map<String,Object> param) throws IOException {

//        String bucketName =(String) param.get("bucketName");
//        String objectName =(String) param.get("objectName");
//
//        if(file==null){
//            return ResponseUtil.errorJSONData("无文件");
//        }
//
//        InputStream inputStream = file.getInputStream();
//        String result="";
//        if(bucketName==null && objectName==null){
//
//            /**
//             *              file
//             *              dog.jpg
//             *              image/jpeg
//             *
//             *             System.out.println(file.getName());
//             *             System.out.println(file.getOriginalFilename());
//             *             System.out.println(file.getContentType());
//             *
//             * */
//
//            result=ossFileService.uploadFile(file.getOriginalFilename(),inputStream);
//        }else if(bucketName == null ){
//            result=ossFileService.uploadFile(inputStream,objectName);
//        }else if(objectName ==null){
//            result=ossFileService.uploadFile(bucketName,inputStream,file.getName());
//        }else{
//            result=ossFileService.uploadFile(inputStream,bucketName,objectName);
//        }

//        return ResponseUtil.successJSONData(result);
            return ResponseUtil.successJSONData("。。");


    }
    
//    注意此处需要权限认证，即管理员才可以
    @PostMapping("/list")
    public String upload(@RequestBody  String bucketName){
        ObjectListing list = ossFileService.list(bucketName);
        for (OSSObjectSummary objectSummary : list.getObjectSummaries()) {
//          TODO 处理列表文件


        }

        return ResponseUtil.successJSONData(list.getObjectSummaries());
    }
    @GetMapping("/test")
    public String test(String path){
        File file = new File(path);
        return ResponseUtil.successJSONData(ossFileService.uploadFile(file));
    }
}
